package com.googlecode.intellimars.sabotter.ui;

import com.googlecode.intellimars.sabotter.SabotterPlugin;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class SabotterPostDialog extends DialogWrapper {
    private JPanel contentPane;
    private JTextArea textArea;
    private JPanel titlePane;
    private JLabel charCount;
    private JPanel statusPane;
    private Project project;

    public SabotterPostDialog(Project _project) {
        super(_project, false);
        project = _project;
        super.init();
        setModal(false);
        setUndecorated(true);
        textArea.setBorder(BorderFactory.createLoweredBevelBorder());
        textArea.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) { panelUpdate(); }
            public void removeUpdate(DocumentEvent e) { panelUpdate(); }
            public void changedUpdate(DocumentEvent e) { panelUpdate(); }
        });
        textArea.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) { dispose(); }
        });
        textArea.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                // ENTERキーはキャンセルする
                if (e.getKeyCode() == KeyEvent.VK_ENTER) e.consume();
                
                // Ctrl+ENTERで送信
                if ((e.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0
                        && e.getKeyCode() == KeyEvent.VK_ENTER) {
                    sendMessage();
                }
            }
        });
        show();
    }

    public SabotterPostDialog(Project project, String id) {
        this(project);
        textArea.setText("@" + id + " ");
    }

    private void panelUpdate() {
        int count = textArea.getText().getBytes().length;
        charCount.setText(String.valueOf(count));
        charCount.setForeground(count > 140 ? Color.RED : Color.BLACK);
        pack();
    }

    private void sendMessage() {
        String text = textArea.getText().replaceAll("\n", "");
        SabotterPlugin plugin = SabotterPlugin.getInstance(project);
        plugin.sendMessage(text);
        dispose();
    }

    protected JComponent createNorthPanel() {
        return titlePane;
    }

    @Nullable
    protected JComponent createCenterPanel() {
        return contentPane;
    }
    protected JComponent createSouthPanel() {
        return statusPane;
    }

    @Nullable
    protected Border createContentPaneBorder() {
        return BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(Color.GRAY),
                    BorderFactory.createEmptyBorder(2,1,2,1));
    }
}
